//
//  LLTEditFrameView.swift
//  mmda
//
//  Created by 李陆涛 on 2018/8/15.
//  Copyright © 2018年 mmda. All rights reserved.
//

import UIKit

let kItemHeight = CGFloat(50)
let kItemWidth = kItemHeight * 2 / 3

class LLTEditFrameView: UIView {
    
    
    private var leftView:UIImageView!
    private var rightView:UIImageView!
    
    var validRect:CGRect = CGRect.zero {
        
        didSet {
            
            leftView.frame = CGRect(x: validRect.origin.x, y: 0, width: kItemWidth / 2, height: kItemHeight)
            
            rightView.frame = CGRect(x: validRect.origin.x + validRect.size.width - kItemWidth / 2, y: 0, width: kItemWidth / 2, height: kItemHeight)
            
            self.setNeedsLayout()
        }
    }
    
    var editViewValidRectChanged:(()->Void)?
    var editViewValidRectEndChanged:(()->Void)?
    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        self.backgroundColor = UIColor.black.withAlphaComponent(0.5)
        self.layer.borderWidth = 2
        self.layer.borderColor = UIColor.clear.cgColor
        
        leftView = UIImageView(image: #imageLiteral(resourceName: "ic_left"))
        leftView.isUserInteractionEnabled = true
        leftView.tag = 0
        self.addSubview(leftView)
        
        leftView.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(panAction(_:))))
        
        
        
        rightView = UIImageView(image: #imageLiteral(resourceName: "ic_right"))
        rightView.isUserInteractionEnabled = true
        rightView.tag = 1
        self.addSubview(rightView)
        
        rightView.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(panAction(_:))))
        
    }
    
    @objc func panAction(_ gesture:UIPanGestureRecognizer) {
        
        self.layer.borderColor = UIColor.white.withAlphaComponent(0.4).cgColor
        
        var point = gesture.location(in: self)
        
        var rect = self.validRect
        
        let w = self.frame.width
        
        var minX = CGFloat(0)
        var maxX = w
        
        switch gesture.view?.tag ?? 0 {
        case 0: // left
            
            maxX = rect.origin.x + rect.size.width - kItemWidth
            point.x = max(minX, min(point.x, maxX))
            point.y = 0
            
            rect.size.width = rect.size.width - (point.x - rect.origin.x)
            rect.origin.x = point.x
        case 1: // right
            
            minX = rect.origin.x + kItemWidth / 2
            maxX = w - kItemWidth / 2
            
            point.x = max(minX, min(point.x, maxX))
            point.y = 0
            
            rect.size.width = (point.x - rect.origin.x + kItemWidth / 2)
        default:
            
            break
        }
        
        switch gesture.state {
        case .began, .changed :
            
            self.editViewValidRectChanged?()
            
        case .ended, .cancelled :
            
            self.layer.borderColor = UIColor.clear.cgColor
            
            self.editViewValidRectEndChanged?()
        default:
            
            break

        }
        
        self.validRect = rect
        
        
    }
    
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        
        //        扩大下有效范围
        var left = leftView.frame
        left.origin.x = left.origin.x - kItemWidth / 2
        
        left.size.width = left.size.width + kItemWidth / 2
        
        var right = rightView.frame
        
        right.size.width = right.size.width + kItemWidth / 2
        
        if left.contains(point) {
            
            return leftView
        }
        
        if right.contains(point) {
            
            return rightView
        }
        
        return nil
    }
    
    
    override func draw(_ rect: CGRect) {
        
        
        let context = UIGraphicsGetCurrentContext()
        
        context?.clear(self.validRect)
        
        context?.setStrokeColor(UIColor.white.cgColor)
        context?.setLineWidth(4.0)
        
        let topPoints = [CGPoint(x:self.validRect.origin.x, y: 0),
                         CGPoint(x: self.validRect.origin.x+self.validRect.size.width, y: 0)]
        
        let bottomPoints = [CGPoint(x:self.validRect.origin.x, y: kItemHeight),
                            CGPoint(x: self.validRect.origin.x+self.validRect.size.width, y: kItemHeight)]
        
        context?.addLines(between: topPoints)
        context?.addLines(between: bottomPoints)
        
        context?.drawPath(using: .stroke)
        
    }
}

